home *** CD-ROM | disk | FTP | other *** search
- /**************************************
- cScorePane.c
-
- SUPERCLASS = CPane
-
- Methods for the score pane. The score pane displays the current player
- turn and calculates and displays the game score.
-
- ****************************************/
-
- #include "cDotsDoc.h"
- #include "cScorePane.h"
- #include "dotsTypes.h"
- #include <Global.h>
-
- /*** Class Constants ***/
- #define kScorePlace 30
- #define kHalfSpacing (kScorePlace / 2)
- #define kScoreSpacing (3*kScorePlace)
- #define kScoreInset 6
-
- /*** Globals ***/
- extern Pattern *dgPlayerPats;
-
- /******** C O N S T R U C T I O N ********/
-
- /*** IScorePane
- *
- * Initialize instance variables
- */
- void cScorePane::IScorePane(CView *anEnclosure, CBureaucrat *aSupervisor,
- short aWidth, short aHeight,
- short aHEncl, short aVEncl,
- SizingOption aHSizing, SizingOption aVSizing)
- {
- /* Highlighted score will be that of the player to move next */
- fPlayerHilite = ((cDotsDoc *)aSupervisor)->getPlayerTurn();
-
- /* Call inherited method */
- IPane(anEnclosure, aSupervisor, aWidth, aHeight,
- aHEncl, aVEncl, aHSizing, aVSizing);
- }
-
- /******** D R A W I N G **********/
-
- /*** Draw {OVERRIDE}
- *
- * The area parameter gives the portion of the
- * pane that needs to be redrawn. Area is in frame coordinates.
- */
- void cScorePane::Draw(Rect *area)
- {
- tScores score;
- Boolean player;
- Rect r;
- Str255 s;
- int offset;
-
- TextFont(monaco);
- TextFace(NULL);
- TextSize(9);
- PenNormal();
-
- /* Draw line across bottom of pane */
- GetFrame(&r);
- MoveTo(r.left, r.bottom-1);
- LineTo(r.right, r.bottom-1);
-
- PenSize(kScoreInset, kScoreInset); /* For frame around scores */
-
- calculateScore(score);
-
- for (player = kPlayer1; player <= kPlayer2; player++) {
- NumToString(score[player], &s);
-
- /* Center score in player's rectangle */
- getPlayerRect(player, &r);
- offset = (r.right - r.left - StringWidth(s)) / 2;
- MoveTo(r.left + offset, r.bottom - 3);
- DrawString(s);
- InsetRect(&r, -kScoreInset, -kScoreInset);
- PenPat(&dgPlayerPats[player]);
- FrameRect(&r);
- }
- /* Hilight/Unhighlight player score */
- getPlayerRect(fPlayerHilite, &r);
- InvertRect(&r);
- }
-
-
- /*** calculateScore
- *
- * Computes the current score
- */
- void cScorePane::calculateScore(tScores theScore)
- {
- tBoxState boxState;
- int row, col;
-
- theScore[kPlayer1] = 0;
- theScore[kPlayer2] = 0;
-
- /* Check box states for entire matrix */
- for (row = 0; row < kMaxRow; row++) {
- for (col = 0; col < kMaxCol; col++) {
- boxState = ((cDotsDoc *)itsSupervisor)->getBoxState(row, col);
- switch (boxState) {
- case kBoxPlayer1:
- theScore[kPlayer1] = theScore[kPlayer1] + 1;
- break;
- case kBoxPlayer2:
- theScore[kPlayer2] = theScore[kPlayer2] + 1;
- break;
- }
- }
- }
- }
-
-
- /*** getPlayerRect
- *
- * Gets rectangle in which to draw player's score
- */
- void cScorePane::getPlayerRect(Boolean thePlayer, Rect *r)
- {
- SetRect(r, kScorePlace, kHalfSpacing, 2*kScorePlace, kScorePlace);
- if (thePlayer == kPlayer2)
- OffsetRect(r, kScoreSpacing, 0);
- }
-
-
- /*** invalScore
- *
- * Invalidate player score rectangle so it will redraw on update
- */
- void cScorePane::invalScore(Boolean thePlayer)
- {
- Rect r;
-
- /* Get rectangle where player score is displayed */
- getPlayerRect(thePlayer, &r);
-
- /* Set to score pane's port before invalidation
- ** otherwise may be drawing in a different port */
- Prepare();
- InvalRect(&r);
- }
-
-
- /*** setTurn
- *
- * Highlight current player's turn.
- * Actually draw on screen since it is easy to update turn indicator.
- */
- void cScorePane::setTurn(Boolean thePlayer)
- {
- Rect r;
-
- if (thePlayer != fPlayerHilite) {
- Prepare(); /* Sets up pane for drawing */
-
- /* Unhighlight currently highlighted player score */
- getPlayerRect(fPlayerHilite, &r);
- InvertRect(&r);
- /* Highlight other player score */
- getPlayerRect(thePlayer, &r);
- InvertRect(&r);
-
- fPlayerHilite = thePlayer;
- }
- }